﻿@model GPS.ENTITYS.CrudModelDKH_LH1

@{
    ViewBag.Title = "Sửa lịch học";
}

@using (Html.BeginForm("Edit", "GPS_LichHoc1", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <h3 class="text-center fdb-title-search-form">Tạo lịch học</h3>
    <div id="tabs">
        <div id="tabs-1">
            @Html.Hidden("IsEdit", 0)
            <label style="color:red">@ViewBag.ErrorDate</label>
            <fieldset class="fs_body">
                <legend class="head_title">Thông tin chi tiết lịch học</legend>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label text-left">
                        Đơn vị đào tạo
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_DVGTVT_MA, (SelectList)ViewBag.DVGTVT_MAs, new { @class = "form-control", @id = "ddlDVGTVT_MA" })
                        @Html.ValidationMessageFor(model => model.KH_LH_DVGTVT_MA)
                    </div>

                    <div class="col-md-2 control-label text-left">
                        Tên khóa học<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_KH_MA, (SelectList)ViewBag.KH_MAs, string.Empty, new { @class = "form-control", @id = "ddlKH_MA" })
                        @Html.ValidationMessageFor(model => model.KH_LH_KH_MA)
                    </div>
                </div>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Ngày KG
                    </div>
                    <div class="col-md-4">
                        @Html.TextBox("txtNGAY_KG", string.Empty, new { @class = "form-control", @readonly = "readonly", @id = "txtNGAY_KG" })
                    </div>
                    <div class="col-md-2 control-label">
                        Ngày BG
                    </div>
                    <div class="col-md-4">
                        @Html.TextBox("txtNGAY_BG", string.Empty, new { @class = "form-control", @readonly = "readonly", @id = "txtNGAY_BG" })
                    </div>
                </div>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Hình thức đào tạo<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_HTDT_MA, (SelectList)ViewBag.HTDT_MAs, string.Empty, new { @class = "form-control", @id = "ddlHTDT_MA" })
                    </div>

                    <div class="col-md-2 control-label">
                        Giai đoạn
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_GDOAN_MA, (SelectList)ViewBag.GD_MAs, string.Empty, new { @class = "form-control" })
                    </div>

                </div>



                <div class="row margin-top-05">

                    <div id="divMONHOC_MA">
                        <div class="col-md-2 control-label">
                            Môn học
                        </div>
                        <div class="col-md-4">
                            @Html.DropDownListFor(model => model.KH_LH_MONHOC_MA, (SelectList)ViewBag.MONHOC_MAs, string.Empty, new { @class = "form-control", @id = "ddlMONHOC_MA" })

                        </div>
                    </div>
                    <div id="divPT_MA">

                        <div class="col-md-2 control-label">
                            Phương tiện
                        </div>
                        <div class="col-md-4">
                            @Html.DropDownListFor(model => model.KH_LH_PT_BIENSO, (SelectList)ViewBag.PT_MAs, string.Empty, new { @class = "form-control", @id = "ddlPT_MA" })

                        </div>
                    </div>
                </div>



                <div class="row margin-top-05">
                    <div class="col-md-2 control-label text-left">
                        Giảng viên<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_GV_MA, (SelectList)ViewBag.GV_MAs, string.Empty, new { @class = "form-control", @id = "ddlGV_MA" })
                        @Html.ValidationMessageFor(model => model.KH_LH_GV_MA)
                    </div>
                    <div class="col-md-2 control-label">
                        Kiểm tra
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_KIEMTRA, new List<SelectListItem>
                                {                   new SelectListItem() {Text = "", Value=""},
                                                    new SelectListItem() {Text = "Kiểm tra", Value="KT"}
                                }, new { @class = "form-control" })
                    </div>
                </div>


                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Tháng
                    </div>
                    <div class="col-md-4">
                        @Html.DropDownListFor(model => model.KH_LH_THANG, new List<SelectListItem>
                                {                   new SelectListItem() {Text = "", Value=""},
                                                    new SelectListItem() {Text = "Tháng 1", Value="1"},
                                                    new SelectListItem() {Text = "Tháng 2", Value="2"},
                                                    new SelectListItem() {Text = "Tháng 3", Value="3"},
                                                    new SelectListItem() {Text = "Tháng 4", Value="4"},
                                                    new SelectListItem() {Text = "Tháng 5", Value="5"},
                                                    new SelectListItem() {Text = "Tháng 6", Value="6"},
                                                    new SelectListItem() {Text = "Tháng 7", Value="7"},
                                                    new SelectListItem() {Text = "Tháng 8", Value="8"},
                                                    new SelectListItem() {Text = "Tháng 9", Value="9"},
                                                    new SelectListItem() {Text = "Tháng 10", Value="10"},
                                                    new SelectListItem() {Text = "Tháng 11", Value="11"},
                                                    new SelectListItem() {Text = "Tháng 12", Value="12"}
                                }, new { @class = "form-control" })
                    </div>

                    <div class="col-md-2 control-label">
                        Tuần
                    </div>
                    <div class="col-md-4">
                        @*@Html.DropDownListFor(model => model.KH_LH_TUAN, new List<SelectListItem>
                            {                   new SelectListItem() {Text = "", Value=""},
                                                new SelectListItem() {Text = "Tuần 01", Value="1"},
                                                new SelectListItem() {Text = "Tuần 02", Value="2"},
                                                new SelectListItem() {Text = "Tuần 03", Value="3"},
                                                new SelectListItem() {Text = "Tuần 04", Value="4"},
                                                new SelectListItem() {Text = "Tuần 05", Value="5"},
                                                new SelectListItem() {Text = "Tuần 06", Value="6"},
                                                new SelectListItem() {Text = "Tuần 07", Value="7"},
                                                new SelectListItem() {Text = "Tuần 08", Value="8"},
                                                new SelectListItem() {Text = "Tuần 09", Value="9"},
                                                new SelectListItem() {Text = "Tuần 10", Value="10"},
                                                new SelectListItem() {Text = "Tuần 11", Value="11"},
                                                new SelectListItem() {Text = "Tuần 12", Value="12"},
                                                new SelectListItem() {Text = "Tuần 13", Value="13"},
                                                new SelectListItem() {Text = "Tuần 14", Value="14"}
                            }, new { @class = "form-control", @id = "ddlKH_LH_TUAN" })*@
                        @Html.DropDownListFor(model => model.KH_LH_TUAN, (SelectList)ViewBag.WEEKS, string.Empty, new { @class = "form-control", @id = "ddlKH_LH_TUAN" })
                    </div>
                </div>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Từ ngày<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @*@Html.TextBoxFor(model => model.KH_LH_TUNGAY, new { @class = "form-control", @id = "txtKH_LH_TUNGAY" })*@
                        @Html.TextBoxFor(model => model.KH_LH_TUNGAY, "{0:dd/MM/yyyy}", new { @class = "form-control", @id = "txtKH_LH_TUNGAY" })
                        @Html.ValidationMessageFor(model => model.KH_LH_TUNGAY)
                    </div>
                    <div class="col-md-2 control-label">
                        Đến ngày<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @*@Html.TextBoxFor(model => model.KH_LH_DENNGAY, new { @class = "form-control", @id = "txtKH_LH_DENNGAY" })*@
                        @Html.TextBoxFor(model => model.KH_LH_DENNGAY, "{0:dd/MM/yyyy}", new { @class = "form-control", @id = "txtKH_LH_DENNGAY" })
                        @Html.ValidationMessageFor(model => model.KH_LH_DENNGAY)
                    </div>
                </div>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Số giờ/ngày<span style="color:red">(*)</span>
                    </div>
                    <div class="col-md-4">
                        @Html.TextBoxFor(model => model.KH_LH_SOGIO, new { @class = "form-control", @type = "text" })
                        <label style="color: #b94a48" id="lblKH_LH_SOGIO_ALERT"></label>
                    </div>
                    <div class="col-md-2 control-label">
                        Số học viên
                    </div>
                    <div class="col-md-4">
                        @Html.TextBoxFor(model => model.KH_LH_SOHOCVIEN, new { @class = "form-control", @type = "number" })
                        @Html.Hidden("txtSO_HOVIEN", new { @class = "form-control", @id = "txtSO_HOVIEN" })
                    </div>
                </div>

                <div class="row margin-top-05">
                    <div class="col-md-2 control-label">
                        Mô tả
                    </div>
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.KH_LH_MOTA, new { @class = "form-group", @rows = 5, @style = "width:100%;max-width:100%;" })
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-md-12">
            <button type="submit" class="btn btn-danger btn-sm" id="btnsubmit">
                <span class="glyphicon glyphicon-floppy-disk"></span> Cập nhật
            </button>
            &nbsp;&nbsp;
            @Html.ActionLink("Quay lại", "Index", null, new { @class = "btn btn-primary btn-sm" })
        </div>
    </div>



}

<div id="wait" style="display: none; position:absolute; top: 10%; right: 10%; padding: 2px; z-index: 99999">
    <img src="~/fonts/images/loading16.gif" width="65" height="65" />
</div>



<link href="~/Content/bootstrap-datetimepicker.min.css" rel="stylesheet" />

<link href="~/dist/css/bootstrap-select.css" rel="stylesheet" />
<script src="~/dist/js/bootstrap-select.js"></script>


<script src="~/Scripts/bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="~/Scripts/locales/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

<script src="~/Scripts/datepicker.js"></script>
<script src="~/Scripts/jquery-ui-1.11.4.js"></script>
<script src="~/Scripts/jquery.maskedinput.js"></script>
<script src="~/Scripts/jquery.maskMoney.js"></script>
<script src="~/Scripts/jquery.number.js"></script>
<script src="~/Scripts/jquery.masknumber.js"></script>
<script src="~/Scripts/moment.js"></script>


<script type="text/javascript">
    // Voi jQuery 1.9 > thi chi validate gia tri trong active tab
    // Muon validate nhung gia tri trong cac tab khac nua thi dung jquery.validate 1.8.1 or 1.7
    // mask edit cho truong ngay thang (su dung jquery.maskedinput)
    function ToJavaScriptDate(value) {
        if (value != null) {
            var pattern = /Date\(([^)]+)\)/;
            var results = pattern.exec(value);
            var dt = new Date(parseFloat(results[1]));
            return dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + dt.getFullYear();
        }
        return null;
    }
</script>

<script type="text/javascript">
    $("#txtKH_LH_TUNGAY").datepicker({
        changeMonth: true,
        changeYear: true,

        dateFormat: 'dd/mm/yy'
    });

    $("#txtKH_LH_DENNGAY").datepicker({
        changeMonth: true,
        changeYear: true,

        dateFormat: 'dd/mm/yy'
    });

    $("#txtNGAY_KG").datepicker({
        changeMonth: true,
        changeYear: true,

        dateFormat: 'dd/mm/yy'
    });

    $("#txtNGAY_BG").datepicker({
        changeMonth: true,
        changeYear: true,

        dateFormat: 'dd/mm/yy'
    });

    //$("#KH_LH_TUNGAY").datetimepicker({ format: 'dd-mm-yyyy hh:ii' });
    //$("#KH_LH_DENNGAY").datetimepicker({ format: 'dd-mm-yyyy hh:ii' });



    $("#txtKH_LH_TUNGAY").mask("99/99/9999", { placeholder: "dd/mm/yyyy" });
    $("#txtKH_LH_DENNGAY").mask("99/99/9999", { placeholder: "dd/mm/yyyy" });

    $("#txtNGAY_KG").mask("99/99/9999", { placeholder: "dd/mm/yyyy" });
    $("#txtNGAY_BG").mask("99/99/9999", { placeholder: "dd/mm/yyyy" });

    // cho phep nhap so thap phan (su dung jquery.number)
    //$("#GV_THAMNIENLX").number(true, 2, '.', '');

    $("#KH_LH_SOGIO").number(true, 2);

</script>



@Styles.Render("~/Content/themes/base/css")

<script type="text/javascript">
    $(document).ready(function () {
        $('.selectpicker').selectpicker({
            liveSearch: true,
            showSubtext: true
        });

        //$('#ddlPT_MA').val("");
        //$('#ddlGV_MA').val("");
        //$("#ddlMONHOC_MA").val("");



        var valHT = $('#ddlHTDT_MA').val();
        var valKH = $('#ddlKH_MA').val();

        if (valHT == null || valHT == '') {
            $("#divMONHOC_MA").attr("style", "display:block");
            $("#divPT_MA").attr("style", "display:block");
        }
        else {
            if (valHT == 'LT') {
                $("#divMONHOC_MA").attr("style", "display:block");
                $("#divPT_MA").attr("style", "display:none");
            }
            else if (valHT == 'TH') {
                $("#divMONHOC_MA").attr("style", "display:none");
                $("#divPT_MA").attr("style", "display:block");
            }
            else {
                $("#divMONHOC_MA").attr("style", "display:block");
                $("#divPT_MA").attr("style", "display:block");
            }
        }


        var valKH = $('#ddlKH_MA').val();
        // alert(valKH);
        if (valKH != null && valKH != '') {
            //   // lstBoxPhuongTien
            //alert(valKH);

            $("#wait").attr("style", "display:block;position:absolute;top:40%;left:50%;padding:2px; z-index:99999");

            $.ajax({
                url: '@Url.Action("getPT_IN_KH_MAs", "GPS_LichHoc1")',
                type: 'POST',
                traditional: true,
                data: {
                    Ma_DVGT: $('#ddlDVGTVT_MA').val(),
                    Ma_KH: $('#ddlKH_MA').val()
                },
                dataType: 'json',
                success: function (data) {
                    //var a = '<option value></option>' + data.pts.Options;   // defaut value
                    //$("#ddlPT_MA").html(a);
                    $("#txtNGAY_KG").val(data.pts.KH_NGAY_KG);
                    $("#txtNGAY_BG").val(data.pts.KH_NGAY_BG);
                    $("#txtSO_HOVIEN").val(data.pts.SO_HOVIEN);


                    $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                },
                error: function () {

                    alert("Lỗi kết nối với server!");
                    $("#wait").attr("style", "display:none;position:absolute;top:20%;left:50%;padding:2px; z-index:99999");

                }
            });

        }

        $("#ddlKH_MA").on("change", function () {
            var valKH = $('#ddlKH_MA').val();
            if (valKH != null && valKH != '') {
                // lstBoxPhuongTien
                var position = $(this).position();

                $("#wait").attr("style", "display:block;position:absolute;top:40%;left:50%;padding:2px; z-index:99999");

                $.ajax({
                    url: '@Url.Action("getPT_IN_KH_MAs", "GPS_LichHoc1")',
                    type: 'POST',
                    traditional: true,
                    data: {
                        Ma_DVGT: $('#ddlDVGTVT_MA').val(),
                        Ma_KH: $('#ddlKH_MA').val()
                    },
                    dataType: 'json',
                    success: function (data) {
                        var a = '<option value></option>' + data.pts.Options;   // defaut value
                        $("#ddlPT_MA").html(a);
                        $("#txtNGAY_KG").val(data.pts.KH_NGAY_KG);
                        $("#txtNGAY_BG").val(data.pts.KH_NGAY_BG);
                        $("#txtSO_HOVIEN").val(data.pts.SO_HOVIEN);

                        var b = '<option value></option>'
                        $('#ddlGV_MA').html(b);
                        $("#ddlHTDT_MA").val("");
                        $("#ddlMONHOC_MA").val("");

                        $("#divMONHOC_MA").attr("style", "display:block");
                        $("#divPT_MA").attr("style", "display:block");

                        $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                    },
                    error: function () {

                        alert("Lỗi kết nối với server!");
                        $("#wait").attr("style", "display:none;position:absolute;top:20%;left:50%;padding:2px; z-index:99999");

                    }
                });


                @*$.ajax({
                    url: '@Url.Action("getPT_IN_KH_MAs", "GPS_LichHoc1")',
                    type: 'post',
                    data: {
                        Ma_DVGT: $('#ddlDVGTVT_MA').val(),
                        Ma_KH: $('#ddlKH_MA').val()
                    }
                }).done(function (data) {
                  //  alert(response);
                    var a = '<option value></option>' + dataresponse;   // defaut value
                    $("#ddlPT_MA").html(a);

                    var b = '<option value></option>'
                    $('#ddlGV_MA').html(b);
                    $("#ddlHTDT_MA").val("");
                    $("#ddlMONHOC_MA").val("");

                    $("#divMONHOC_MA").attr("style", "display:block");
                    $("#divPT_MA").attr("style", "display:block");

                    $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                });*@
            }


        });


        $("#ddlHTDT_MA").on("change", function () {

            $('#ddlPT_MA').val("");
            $('#ddlGV_MA').val("");
            $("#ddlMONHOC_MA").val("");

            var valHT = $('#ddlHTDT_MA').val();
            var valKH = $('#ddlKH_MA').val();

            if (valHT == null || valHT == '') {
                $("#divMONHOC_MA").attr("style", "display:block");
                $("#divPT_MA").attr("style", "display:block");
            }
            else {
                if (valHT == 'LT') {
                    $("#divMONHOC_MA").attr("style", "display:block");
                    $("#divPT_MA").attr("style", "display:none");
                    $("#KH_LH_SOHOCVIEN").val($("#txtSO_HOVIEN").val());
                }
                else if (valHT == 'TH') {
                    $("#divMONHOC_MA").attr("style", "display:none");
                    $("#divPT_MA").attr("style", "display:block");
                }
                else {
                    $("#divMONHOC_MA").attr("style", "display:block");
                    $("#divPT_MA").attr("style", "display:block");
                }
            }

            if (valHT != null && valHT != '' && valKH != null && valKH != '') {
                // lstBoxPhuongTien
                var position = $(this).position();

                $("#wait").attr("style", "display:block;position:absolute;top:40%;left:50%;padding:2px; z-index:99999");

                $.ajax({
                    url: '@Url.Action("getGV_IN_KH_BY_HTHUCDTs", "GPS_LichHoc1")',
                    type: 'post',
                    data: {
                        Ma_DVGT: $('#ddlDVGTVT_MA').val(),
                        Ma_KH: $('#ddlKH_MA').val(),
                        Ma_HT: $('#ddlHTDT_MA').val()
                    }
                }).done(function (response) {
                    // alert(response);
                    var a = '<option value></option>' + response;   // defaut value
                    $("#ddlGV_MA").html(a);

                    $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                });


            }


        });

        $("#ddlPT_MA").on("change", function () {
            var valHT = $('#ddlHTDT_MA').val();
            var valKH = $('#ddlKH_MA').val();
            var valPT = $('#ddlPT_MA').val();

            if (valHT != null && valHT != '' && valKH != null && valKH != '') {
                // lstBoxPhuongTien
                var position = $(this).position();

                $("#wait").attr("style", "display:block;position:absolute;top:40%;left:50%;padding:2px; z-index:99999");

                $.ajax({
                    url: '@Url.Action("getGV_IN_THUCHANH_HTHUCDTs", "GPS_LichHoc1")',
                    type: 'post',
                    data: {
                        Ma_DVGT: $('#ddlDVGTVT_MA').val(),
                        Ma_KH: $('#ddlKH_MA').val(),
                        Ma_HT: $('#ddlHTDT_MA').val(),
                        Pt_MA: $('#ddlPT_MA').val()
                    }
                }).done(function (response) {
                    // alert(response);
                    var a = '<option value></option>' + response;   // defaut value
                    $("#ddlGV_MA").html(a);

                    $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                });


            }


        });


        //Week:

        $("#ddlKH_LH_TUAN").on("change", function () {
            var valKH = $('#ddlKH_MA').val();
            var week = $(this).val();

            if (valKH != null && valKH != '') {

                $("#wait").attr("style", "display:block;position:absolute;top:40%;left:50%;padding:2px; z-index:99999");

                $.ajax({
                    url: '@Url.Action("GPS_GET_DATE_BY_WEEK", "GPS_LichHoc1")',
                    type: 'POST',
                    traditional: true,
                    data: {
                        Week: week,
                        StartDate: $('#txtNGAY_KG').val(),
                        EndDate: $('#txtNGAY_BG').val()
                    },
                    dataType: 'json',
                    success: function (data) {
                        $("#txtKH_LH_TUNGAY").val(data.dateranges.StrStartDate);
                        $("#txtKH_LH_DENNGAY").val(data.dateranges.StrEndDate);

                        $("#wait").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
                    },
                    error: function () {

                        alert("Lỗi kết nối với server!");
                        $("#wait").attr("style", "display:none;position:absolute;top:20%;left:50%;padding:2px; z-index:99999");

                    }
                });
            }
        });


        $("#KH_LH_SOGIO").on("blur", function () {
            var sogio = $(this).val();
            if (sogio != null && sogio != '') {
                var sogioInt = parseFloat(sogio);
                if (sogioInt > 8) {
                    $("#lblKH_LH_SOGIO_ALERT").text("Giảng viên số giờ /ngày phải nhỏ hơn hoặc bằng 8, Phương tiện số giờ /ngày phải nhỏ hơn hoặc bằng 12 ");
                }
                else if (sogioInt <= 0) {
                    $("#lblKH_LH_SOGIO_ALERT").text("Số giờ không được nhỏ hơn 0");
                }
                else {
                    $("#lblKH_LH_SOGIO_ALERT").text("");
                }
            }
        });

    });



</script>

<script type="text/javascript">
    function getDateRangeOfWeek(weekNo) {
        var d1 = new Date();
        numOfdaysPastSinceLastMonday = eval(d1.getDay() - 1);
        d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
        var weekNoToday = d1.getWeek();
        var weeksInTheFuture = eval(weekNo - weekNoToday);
        d1.setDate(d1.getDate() + eval(7 * weeksInTheFuture));
        var rangeIsFrom = eval(d1.getMonth() + 1) + "/" + d1.getDate() + "/" + d1.getFullYear();
        d1.setDate(d1.getDate() + 6);
        var rangeIsTo = eval(d1.getMonth() + 1) + "/" + d1.getDate() + "/" + d1.getFullYear();
        return rangeIsFrom + " to " + rangeIsTo;
    };

    Date.prototype.getWeek = function () {

        // Create a copy of this date object
        var target = new Date(this.valueOf());

        // ISO week date weeks start on monday, so correct the day number
        var dayNr = (this.getDay() + 6) % 7;

        // Set the target to the thursday of this week so the
        // target date is in the right year
        target.setDate(target.getDate() - dayNr + 3);

        // ISO 8601 states that week 1 is the week with january 4th in it
        var jan4 = new Date(target.getFullYear(), 0, 4);

        // Number of days between target date and january 4th
        var dayDiff = (target - jan4) / 86400000;

        if (new Date(target.getFullYear(), 0, 1).getDay() < 5) {
            // Calculate week number: Week 1 (january 4th) plus the
            // number of weeks between target date and january 4th
            return 1 + Math.ceil(dayDiff / 7);
        }
        else {  // jan 4th is on the next week (so next week is week 1)
            return Math.ceil(dayDiff / 7);
        }
    };
</script>
